package com.tronsis.distribution.activity.me;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.tronsis.distribution.R;
import com.tronsis.distribution.bean.ProductDetailBean;
import com.tronsis.distribution.biz.factory.ProductBizFactory;
import com.tronsis.library.activity.ToolBarBaseActivity;
import com.tronsis.library.adapter.recyclerview.CommonAdapter;
import com.tronsis.library.adapter.recyclerview.base.ViewHolder;
import com.tronsis.library.http.UICallBack;
import com.tronsis.library.util.DisplayUtil;
import com.tronsis.library.util.LogUtil;
import com.tronsis.library.util.ToastUtil;
import com.tronsis.library.widget.MyDecoration;
import com.tronsis.library.widget.RecycleViewDivider;
import com.tronsis.library.zxing.CaptureActivity;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class QrResultActivity extends ToolBarBaseActivity {

    @BindView(R.id.rv_product)
    RecyclerView mRvProduct;


    private CommonAdapter<ProductDetailBean> mAdapter;
    private List<ProductDetailBean> mProductList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_qr_result);
        ButterKnife.bind(this);
        setTitle(true, getString(R.string.qr_product), getString(R.string.next));

        String qrResult = getIntent().getStringExtra(CaptureActivity.EXTRA_QR_RESULT);
        LogUtil.e("qrResult", "qrResult=" + qrResult);
        httpFindProduct(qrResult);
        initRv();
    }

    private void initRv() {
        mAdapter = new CommonAdapter<ProductDetailBean>(mContext, mProductList, R.layout.item_qr_product) {
            @Override
            protected void convert(ViewHolder holder, ProductDetailBean bean, int position) {
                holder.setText(R.id.tv_num, String.valueOf(position + 1));
                holder.setText(R.id.tv_product_num, bean.getQrCode());
                holder.setText(R.id.tv_product_name, bean.getName());
            }
        };

        int height = DisplayUtil.getInstance(mContext).dp2px(1);
        int color = Color.parseColor("#F4F4F4");
        RecycleViewDivider decoration = new RecycleViewDivider(mContext, MyDecoration.VERTICAL_LIST, height, color);
        mRvProduct.addItemDecoration(decoration);
        mRvProduct.setLayoutManager(new LinearLayoutManager(mContext));
        mRvProduct.setAdapter(mAdapter);
    }

    @Override
    protected void onToolbarClicked(int paramId) {
        super.onToolbarClicked(paramId);
        switch (paramId) {
            case RIGHT_TEXT:
                if (mProductList.isEmpty()) {
                    ToastUtil.showMessage(mContext,"未查询到产品");
                    return;
                }
                ProductDetailBean productDetailBean = mProductList.get(0);
                Intent intent = new Intent(mContext, DeliverInfoEditActivity.class);
                intent.putExtra("ProductDetailBean", productDetailBean);
                startActivity(intent);
                break;
        }

    }

    @OnClick(R.id.btn_continue)
    void continueClick() {
        //        Intent intent = new Intent(mContext, DeliverInfoEditActivity.class);
        //        startActivity(intent);
    }

    private void httpFindProduct(final String qrCode) {
        if (qrCode == null) {
            return;
        }
        final String[] split = qrCode.split("=");
        try {
            ProductBizFactory.createBiz().find(this, qrCode, new UICallBack() {
                @Override
                public void onStart() {
                    showLoadingDialog();
                }

                @Override
                public void onRefresh(Object data) {
                    dismissLoadingDialog();
                    ProductDetailBean productDetailBean = (ProductDetailBean) data;
                    if (split.length > 1) {
                        productDetailBean.setQrCode(split[1]);
                    }
                    mProductList.add(productDetailBean);
                    mAdapter.notifyDataSetChanged();
                }

                @Override
                public void onFailure(int status) {
                    dismissLoadingDialog();
                }
            });
        } catch (Exception e) {
            dismissLoadingDialog();
            e.printStackTrace();
        }
    }

}
